package com.iraeta.controller.system;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.iraeta.common.Result;
import com.iraeta.common.ServiceException;
import com.iraeta.controller.BaseController;
import com.iraeta.entity.main.CustomOrg;
import com.iraeta.entity.main.Role;
import com.iraeta.entity.main.SysOrguser;
import com.iraeta.entity.main.User;
import com.iraeta.service.sys.ISysOrguserService;
import com.iraeta.service.sys.RoleService;
import com.iraeta.service.sys.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController(value = "UserAdminController")
@RequestMapping("sys/useradmin")
public class UserAdminController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(UserAdminController.class);
    @Autowired
    UserService userService;
    @Resource
    ISysOrguserService sysOrguserService;
    @Autowired
    RoleService roleService;

    @RequestMapping("pageuser")
    public String pageUser(@RequestBody JSONObject jo){
        String userName = jo.getString("userName");
        this.pageSet(jo);
        List<User> userList = userService.listUserByName(userName);
        PageInfo<User> userPageInfo = this.getPageInfo(userList);
        return JSON.toJSONString(Result.success(userPageInfo));
    }

    @RequestMapping("pageorguser")
    public String pageOrgUser(@RequestBody JSONObject jo){
        Integer orgId = jo.getIntValue("orgId");
        String searchName = jo.getString("searchName");
        this.pageSet(jo);
        List<User> userList = userService.listOrgUserByName(orgId, searchName);
        PageInfo<User> userPageInfo = this.getPageInfo(userList);
        return JSON.toJSONString(Result.success(userPageInfo));
    }

    @RequestMapping("getuser")
    public String getUser(@RequestBody JSONObject paramJb){
        int userId = paramJb.getIntValue("id");
        User user = userService.getUser(userId);
        return JSON.toJSONString(Result.success(user));
    }

    @RequestMapping("addedituser")
    public String addUser(@RequestBody JSONObject paramJb){
        int userId = paramJb.getIntValue("id");
        User user = userId == 0 ? new User() :  userService.getUser(userId);
        this.mapJsonToObj(paramJb, user);
        user.setPhoto("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        if(userId == 0){
            int orgId = paramJb.getIntValue("orgId");
            userService.addUser(user);
            SysOrguser sysOrguser = new SysOrguser(orgId == 0 ? CustomOrg.root : orgId, user.getId());
            sysOrguserService.save(sysOrguser);
        }else {
            userService.updateUser(user);
        }
        return JSON.toJSONString(Result.success());
    }

    /*user role相关*/
    @RequestMapping("listuserrole")
    public String listUserRole(@RequestBody JSONObject paramJb){
        int userId = paramJb.getIntValue("userId");
        List<Role> allRoles = roleService.listAllRole();
        List<Role> userRoles = roleService.listRoleByUserId(userId);
        JSONObject data = new JSONObject();
        data.put("allRoles", allRoles);
        data.put("userRoles", userRoles);
        return JSON.toJSONString(Result.success(data));
    }

    @RequestMapping("saveuserrole")
    public String saveUserRole(@RequestBody JSONObject paramJb){
        int userId = paramJb.getIntValue("userId");
        JSONArray userRolesJA = paramJb.getJSONArray("userRoles");
        List<Integer> userRoles = new ArrayList<>();
        for(int i =0; i<userRolesJA.size(); i++){
            userRoles.add(userRolesJA.getIntValue(i));
        }
        try {
            userService.saveUserRole(userId, userRoles);
            return JSON.toJSONString(Result.success());
        }catch (Exception e){
            throw new ServiceException(e.getMessage());
        }
    }
}
